package initAll;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.events.WebDriverEventListener;

import com.google.common.io.Files;


public class eventsListener implements WebDriverEventListener{
	private static final Logger log = LogManager.getLogger(eventsListener.class.getName());
	
	@Override
	public void onException(Throwable throwable, WebDriver driver) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        String dateString = format.format(new Date());
        
		String fileName="test-output/"+dateString+".png";
		File scrFile = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE);
		try {
			Files.copy(scrFile,new File(fileName));
		} catch (IOException e) {
			e.printStackTrace();
		}
        
        log.error("cause by:"+throwable.getMessage());
        log.error("image filePath:"+fileName);      
	}

	
	@Override
	public void afterFindBy(By by, WebElement element, WebDriver driver) {
		log.debug("afterFindBy; element-Info="+element.toString());
	}

	@Override
	public void afterClickOn(WebElement element, WebDriver driver) {
		log.debug("afterClickOn; element-Info="+element.toString());
	}

	@Override
	public void afterNavigateForward(WebDriver driver) {
		log.debug("afterNavigateForward; URL=" + driver.getCurrentUrl() + "  title=" + driver.getTitle());
	}
	

	@Override
	public void afterSwitchToWindow(String windowName, WebDriver driver) {
		log.debug("afterSwitchToWindow;  URL=" + driver.getCurrentUrl() + "  title=" + driver.getTitle());
	}

	@Override
	public void afterNavigateTo(String url, WebDriver driver) {
		// TODO Auto-generated method stub
		log.debug("afterNavigateTo;URL="+url);	
	}
	
	@Override
	public void beforeNavigateForward(WebDriver driver) {
		// TODO Auto-generated method stub
	}
	
	@Override
	public void beforeClickOn(WebElement element, WebDriver driver) {
		// TODO Auto-generated method stub
	}
	
	@Override
	public void beforeNavigateTo(String url, WebDriver driver) {
		// TODO Auto-generated method stub
	}
	
	@Override
	public void beforeAlertAccept(WebDriver driver) {
		// TODO Auto-generated method stub
	}

	@Override
	public void afterAlertAccept(WebDriver driver) {
		// TODO Auto-generated method stub
	}

	@Override
	public void afterAlertDismiss(WebDriver driver) {
		// TODO Auto-generated method stub	
	}

	@Override
	public void beforeAlertDismiss(WebDriver driver) {
		// TODO Auto-generated method stub
	}

	@Override
	public void beforeNavigateBack(WebDriver driver) {
		// TODO Auto-generated method stub	
	}

	@Override
	public void afterNavigateBack(WebDriver driver) {
		// TODO Auto-generated method stub	
	}


	@Override
	public void beforeNavigateRefresh(WebDriver driver) {
		// TODO Auto-generated method stub
	}

	@Override
	public void afterNavigateRefresh(WebDriver driver) {
		// TODO Auto-generated method stub
	}

	@Override
	public void beforeFindBy(By by, WebElement element, WebDriver driver) {
		// TODO Auto-generated method stub
	}

	@Override
	public void beforeChangeValueOf(WebElement element, WebDriver driver, CharSequence[] keysToSend) {
		// TODO Auto-generated method stub
	}

	@Override
	public void afterChangeValueOf(WebElement element, WebDriver driver, CharSequence[] keysToSend) {
		// TODO Auto-generated method stub
	}

	@Override
	public void beforeScript(String script, WebDriver driver) {
		// TODO Auto-generated method stub
	}

	@Override
	public void afterScript(String script, WebDriver driver) {
		// TODO Auto-generated method stub
	}

	@Override
	public void beforeSwitchToWindow(String windowName, WebDriver driver) {
		// TODO Auto-generated method stub
	}

	@Override
	public <X> void beforeGetScreenshotAs(OutputType<X> target) {
		// TODO Auto-generated method stub
	}

	@Override
	public <X> void afterGetScreenshotAs(OutputType<X> target, X screenshot) {
		// TODO Auto-generated method stub
	}

	@Override
	public void beforeGetText(WebElement element, WebDriver driver) {
		// TODO Auto-generated method stub
	}

	@Override
	public void afterGetText(WebElement element, WebDriver driver, String text) {
		// TODO Auto-generated method stub
	}

}
